package jdk8.methodres;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

public class MethodRes {
    public static void main(String args[]){
        List<String> names = new ArrayList();

        names.add("Google");
        names.add("Runoob");
        names.add("Taobao");
        names.add("Baidu");
        names.add("Sina");

        names.forEach(System.out::println);
//        names.forEach(str -> System.out.println(str));
        names.forEach( new Consumer(){
            @Override
            public void accept(Object o) {

            }
        });
        System.out.println();
//        Collections.sort(names);
//        Collections.sort(names, (o1, o2)-> o1.length()-o2.length() );   //排序根据长度，从短到长
        names.sort(Comparator.comparingInt(String::length));   //排序根据长度，从短到长

        names.forEach(System.out::println);
    }

    @Test
    public void test(){
        List<Person> personVO = new ArrayList();

        List<Person> personList = new ArrayList();
        int a = 10;

        personList.add(new Person(10, "c"));
        personList.add(new Person(6, "e"));
        personList.add(new Person(13, "b"));
        personList.add(new Person(8, "d"));
        personList.add(new Person(18, "a"));
        personList.add(new Person(3, "f"));

        personList.sort(Comparator.comparingInt(Person::getAge));
        personList.forEach( person -> {
            personVO.add(person);
//            System.out::println;
        });
        System.out.println();
        personList.forEach(new Consumer(){
            @Override
            public void accept(Object o) {
//                System.out.println(((Person)o).toString());
                System.out.println(((Person)o).toString());
            }
        });

    }


}

class Person {
    private int age;
    private String name;

    Person(int age, String name){
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString(){
        return  age + "岁的" + name;
    }
}